package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.AdMaterialCommonBean;
import com.hq.fxh.Modle.GetEnterpriseMaterialRes;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.R;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class TempleteSourceNewActivity extends BaseActivity {

    private int uid;
    private List<AdMaterialCommonBean> localTemplateList = new ArrayList<>();
    private ImageView back;
    private int PageNum = 10;
    private int PageCurrent = 1;
    private SwipeRefreshLayout refresh;
    private RecyclerView recyclerView;
    private AdMaterialAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_templete_source_new;
    }

    @Override
    protected void initView() {
        back = (ImageView)findViewById(R.id.back);
        refresh = (SwipeRefreshLayout)findViewById(R.id.refresh);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
    }

    @Override
    protected void initData() {
        uid = SharedPreferenceUtils.getMemberId(TempleteSourceNewActivity.this);
        getTempleteSource();

        LinearLayoutManager layoutManager = new LinearLayoutManager(TempleteSourceNewActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
    /*    refresh.setColorSchemeColors(getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.colorPrimary));
        refresh.setDirection(SwipeRefreshLayoutDirection.BOTH);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipeRefreshLayoutDirection direction) {
                if (direction == SwipeRefreshLayoutDirection.TOP) {//刷新
                    PageCurrent = 1;
                    getTempleteSource();
                } else {//加载更多
                    PageCurrent++;
                    getTempleteSource();
                }
            }
        });*/
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(this,"企业素材");
    }

    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(this,"企业素材");
    }

    public void getTempleteSource(){
        UserServiceImpl.getEnterpriseMaterial(uid+"",PageCurrent,PageNum,new NetworkSubscriber<GetEnterpriseMaterialRes>(){
            @Override
            public int onSuccess(GetEnterpriseMaterialRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    if (PageCurrent == 1) {
                        localTemplateList.clear();
                    }
                    localTemplateList.addAll(data.getData());
                    if (adapter == null) {
                        adapter = new AdMaterialAdapter(TempleteSourceNewActivity.this, localTemplateList,uid,false);
                        recyclerView.setAdapter(adapter);
                    }
                    adapter.notifyDataSetChanged();
                    refresh.setRefreshing(false);
                } else {
                    ToastUtils.show(TempleteSourceNewActivity.this,data.getMsg());
                }
                return 0;
            }
        });

    }
}
